﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialFront.Entities;
using SocialFront.Providers.Blog;
using SocialFront.Providers.Member;
using SocialFront.Providers.Comment;
using umbApi = umbraco;

namespace SocialFront.Web.usercontrols.Shared
{
    public partial class AddComment : System.Web.UI.UserControl
    {
        public int ItemId { get; set; }

        protected void Page_Init(object sender, EventArgs e)
        {
            ((umbApi.UmbracoDefault)Page).ValidateRequest = false;
            Utilities.AddTinyMce();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (MemberProvider.Provider.GetCurrentMember() == null)
            {
                // Please log in
                MultiView1.ActiveViewIndex = 2;
            }
            else
            {
                // Add a comment
                MultiView1.ActiveViewIndex = 0;
            }
        }

        protected void Submit_OnClick(object sender, EventArgs e)
        {
            // Get a reference to the item being commented on and add a comment node to it
            if (ItemId > 0)
            {
                Entities.Member m = MemberProvider.Provider.GetCurrentMember();

                if (m != null)
                {
                    //User systemUser = User.GetUser(0);

                    //// Create a new document and save it - but don't publish it!
                    //Document d = Document.MakeNew(m.LoginName, DocumentType.GetByAlias("comment"), systemUser,
                    //                 ItemId);
                    //d.Text = m.LoginName;
                    //d.getProperty("content").Value = Comments.Text;
                    //d.getProperty("title").Value = Title.Text;
                    //d.Save();

                    //// Relate the new node to the member
                    //Relation.MakeNew(m.Id, d.Id, RelationType.GetByAlias("relateMemberToCommentDocType"),
                    //                 String.Format("Relating comment {0} to member {1}", d.Id,
                    //                               m.LoginName));

                    Comment comment = new Comment
                    {
                        Title = Title.Text,
                        Content = Comments.Text
                    };

                    comment = CommentProvider.Provider.CreateComment(comment);



                    // Thank you
                    MultiView1.ActiveViewIndex = 1;
                }
                else
                {
                    // Please log in
                    MultiView1.ActiveViewIndex = 2;    

                }

            }
        }
    }
}